package com.zzvcom.webapp.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.zzvcom.template.ResultPage;
import com.zzvcom.webapp.action.admin.LogImgAction;
import com.zzvcom.webapp.action.web.NewsViewAction;
import com.zzvcom.webapp.action.web.ProductViewAction;

@Controller
@Scope("prototype")
// 声明此类为控制层的类,且为prototype/singleton模式调用
public class IndexAction
{
    private ResultPage hotPage;

    private ResultPage newsPageL;

    private ResultPage newsPageR;

    private ResultPage logPage;

    private ProductViewAction view;

    private NewsViewAction news;

    private LogImgAction log;

    // here for the cache
    private static final Object LOCK = new Object();

    private static ResultPage HOTPAGE = null;

    private static ResultPage NEWSPAGE_L = null;

    private static ResultPage NEWSPAGE_R = null;

    private static ResultPage LOGPAGE = null;

    private static Object FLAG = null;

    public static void clearCache()
    {
        synchronized (LOCK)
        {
            FLAG = null;
        }

    }

    private void initCacheValue()
    {

        hotPage = HOTPAGE;
        newsPageL = NEWSPAGE_L;
        newsPageR = NEWSPAGE_R;
        logPage = LOGPAGE;
    }

    public String execute()
    {
        if (FLAG == null)
        {
            synchronized (LOCK)
            {
                if (FLAG != null)
                {
                    initCacheValue();
                    return "index";
                }
                hotPage = new ResultPage();
                hotPage.setPageSize(12);
                view.setResultPage(hotPage);
                view.setParams("hot-0");
                view.c();
                newsPageL = new ResultPage();
                newsPageL.setPageSize(5);
                newsPageR = new ResultPage();
                newsPageR.setPageSize(5);
                news.setResultPage(newsPageL);
                news.queryByType("1");
                news.setResultPage(newsPageR);
                news.queryByType("2");
                logPage = new ResultPage();
                log.setResultPage(logPage);
                log.execute();

                HOTPAGE = hotPage;
                NEWSPAGE_L = newsPageL;
                NEWSPAGE_R = newsPageR;
                LOGPAGE = logPage;
                FLAG = new Object();
            }
        }
        else
        {
            initCacheValue();
        }

        return "index";
    }

    public ResultPage getHotPage()
    {
        return hotPage;
    }

    public void setHotPage(ResultPage hotPage)
    {
        this.hotPage = hotPage;
    }

    public ProductViewAction getView()
    {
        return view;
    }

    @Autowired
    public void setView(ProductViewAction view)
    {
        this.view = view;
    }

    public ResultPage getNewsPageL()
    {
        return newsPageL;
    }

    public void setNewsPageL(ResultPage newsPageL)
    {
        this.newsPageL = newsPageL;
    }

    public ResultPage getNewsPageR()
    {
        return newsPageR;
    }

    public void setNewsPageR(ResultPage newsPageR)
    {
        this.newsPageR = newsPageR;
    }

    public NewsViewAction getNews()
    {
        return news;
    }

    @Autowired
    public void setNews(NewsViewAction news)
    {
        this.news = news;
    }

    public ResultPage getLogPage()
    {
        return logPage;
    }

    public void setLogPage(ResultPage logPage)
    {
        this.logPage = logPage;
    }

    public LogImgAction getLog()
    {
        return log;
    }

    @Autowired
    public void setLog(LogImgAction log)
    {
        this.log = log;
    }

}
